package com.jokul.service;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.jokul.dao.AccountDao;
import com.jokul.dao.BlahDao;
import com.jokul.model.Account;
import com.jokul.model.Blah;

/**
 * @author: jokul
 * @2013-3-15 下午3:29:12
 */

public class UserService {

	private LinkedList<Blah> neweat = new LinkedList<Blah>();
	private AccountDao accountDao;
	private BlahDao blahDao;

	public UserService(String USERS, AccountDao accountDao, BlahDao blahDao) {
		this(accountDao, blahDao);
	}

	public UserService(AccountDao accountDao, BlahDao blahDao) {
		super();
		this.accountDao = accountDao;
		this.blahDao = blahDao;
	}

	public UserService() {
		super();
	}

	/**
	 * 判断用户是否存在
	 * @param account
	 * @return
	 */
	public boolean isUserExisted(Account account) {
		if (StringUtils.isNotEmpty(account.getName())
				&& StringUtils.isNotEmpty(account.getPassword())) {
			Account account2 = accountDao.getAccount(account);
			return account2 != null && account.getPassword().equals(account2.getPassword()); 
		}
		return false;
	}
	
	/**
	 * 根据条件返回所有信息
	 * @param blah
	 * @return
	 */
	public List<Blah> getBlahs(Blah blah) {
		List<Blah> blahs = blahDao.getBlahs(blah);
		return blahs;
	}
	
	/**
	 * 添加信息
	 * @param blah
	 * @return
	 */
	public int addBlah(Blah blah) {
		int result = blahDao.addBlah(blah);
		neweat.addFirst(blah);
		if(neweat.size() > 20) {
			neweat.removeLast();
		}
		return result;
	}

	public int deleteBlah(Blah blah) {
		neweat.remove(blah);
		return blahDao.deleteBlah(blah);
	}
	
	public List<Blah> getNeweat() {
		return neweat;
	}
	
	/**
	 * 添加用户
	 * @param account
	 * @return
	 */
	public int addAccount(Account account) {
		return accountDao.addAccount(account);
	}
}
